from django.db import models
from django.db.models import permalink
from tagging.fields import TagField


class PlaceType(models.Model):
  """ Place types """
  title         = models.CharField(max_length=100, unique=True)
  slug          = models.SlugField(prepopulate_from=('title',), unique=True)
  
  class Meta:
    verbose_name_plural = 'place types'
    db_table = 'place_types'
  
  class Admin:
    pass
    
  def __unicode__(self):
    return '%s' % self.title
  
  @permalink
  def get_absolute_url(self):
    return ('place_type_detail', None, { 'slug': self.slug })


class City(models.Model):
  """ City model """
  city          = models.CharField(max_length=100)
  state         = models.USStateField()
  slug          = models.SlugField(prepopulate_from=('city','state',), unique=True)
  
  class Meta:
    verbose_name_plural = 'cities'
    db_table = 'place_cities'
    unique_together = (('city', 'state',),)
    ordering = ('state', 'city',)
  
  class Admin:
    pass
  
  def __unicode__(self):
    return '%s, %s' % (self.city, self.state)
  
  @permalink
  def get_absolute_url(self):
    return ('place_city_detail', None, { 'slug': self.slug })


class Point(models.Model):
  """ Point model """
  latitude      = models.FloatField(blank=True, null=True)
  longitude     = models.FloatField(blank=True, null=True)
  address       = models.CharField(max_length=200, blank=True)
  city          = models.ForeignKey(City)
  zip           = models.CharField(max_length=10, blank=True)
  country       = models.CharField(blank=True, max_length=100)
  
  class Meta:
    db_table = 'place_points'
    ordering = ('address',)
  
  class Admin:
    list_display  = ('address', 'city', 'zip', 'latitude', 'longitude')
    list_filter   = ('city',)
    search_fields = ('address',)
  
  def __unicode__(self):
    return '%s' % self.address


class Place(models.Model):
  """ Place model """
  STATUS_CHOICES = (
    (0, 'Inactive'),
    (1, 'Active'),
  )
  point         = models.ForeignKey(Point)
  prefix        = models.CharField('Pre-name', blank=True, max_length=20)
  title         = models.CharField(max_length=255)
  slug          = models.SlugField(prepopulate_from=('title',))
  nickname      = models.CharField(blank=True, max_length=100)
  unit          = models.CharField(blank=True, max_length=100, help_text='Suite or Apartment #')
  phone         = models.CharField(blank=True, max_length=20)
  url           = models.URLField(blank=True, verify_exists=False)
  email         = models.EmailField(blank=True)
  description   = models.TextField(blank=True)
  status        = models.IntegerField(choices=STATUS_CHOICES, radio_admin=True, default=1)
  created       = models.DateTimeField(auto_now_add=True)
  modified      = models.DateTimeField(auto_now=True)
  place_types   = models.ManyToManyField(PlaceType, blank=True)
  tags          = TagField()
  
  class Meta:
    db_table = 'places'
    ordering = ('title',)
    
  class Admin:
    list_display  = ('title', 'point', 'city', 'status')
    list_filter   = ('status', 'place_types')
    search_fields = ('title',)
  
  def __unicode__(self):
    return '%s' % self.full_title
  
  @property
  def city(self):
    return self.point.city
  
  @property
  def full_title(self):
    return '%s %s' % (self.prefix, self.title)
  
  @permalink
  def get_absolute_url(self):
    return ('place_detail', None, { 'slug': self.slug } )
  
  @property
  def address(self):
    return '%s, %s %s' % (self.point.address, self.point.city, self.point.zip)